package com.yulongtian.offers.day202209.day20220923;

/**
 * https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/?favorite=xb9nqhhg
 *
 * @author YulongTian
 * @date 2022/9/23 - 15:56
 */
public class Test03 {
    public static void main(String[] args) {
        int[] nums={1,2,3,4};
        int[] exchange = exchange(nums);
        for (int num : exchange) {
            System.out.print(num+",");
        }
        System.out.println();


    }

    public static int[] exchange(int[] nums) {
        if(nums.length<=1){
            return nums;
        }
        int len = nums.length;

        int left = 0;
        int right = len - 1;

        while (left < right) {

            //奇  偶
            if (nums[left] % 2 == 1 && nums[right] % 2 == 0) {
                left++;
                right--;
                continue;
            }
            //奇  奇
            if (nums[left] % 2 == 1 && nums[right] % 2 == 1) {
                left++;
                continue;
            }

            //偶  偶
            if (nums[left] % 2 == 0 && nums[right] % 2 == 0) {
                right--;
                continue;
            }

            //偶  奇
            if (nums[left] % 2 == 0 && nums[right] % 2 == 1) {
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
                left++;
                right--;
                continue;
            }
        }

        return nums;
    }
}
